package cn.lzx.service.impl;

import cn.lzx.domain.JsonResult;
import cn.lzx.domain.MovieTicket;

import cn.lzx.domain.User;
import cn.lzx.dto.TicketInfo;
import cn.lzx.dto.UserMovieDto;
import cn.lzx.feign.MovieFeign;

import cn.lzx.mapper.UserMapper;
import cn.lzx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MovieFeign movieFeign;


    @Override
    public void buyTicket(UserMovieDto userMovieDto, MovieTicket movieTicket) {
        //获取用户
        Long userId = userMovieDto.getUserId();
        User user = userMapper.getUserById(userId);
        //获取用户的金额
        BigDecimal balance = user.getBalance();
        Integer count = userMovieDto.getCount();
        //获取电影的价格
        BigDecimal price = movieTicket.getPrice();
        //用户金额减去购买票数乘票价
        BigDecimal subtract = balance.subtract(price.multiply(new BigDecimal(count)));
        //判断用户余额是否足够
        if (subtract.doubleValue() <0) {
            throw new RuntimeException("你的余额不足。");
        }
        //更新用户余额信息
        user.setBalance(subtract);
        userMapper.update(user);

    }

    @Override
    public TicketInfo getTicket(Long id) {
        //获取电影票信息
        TicketInfo ticketInfo = new TicketInfo();
        JsonResult ticket = movieFeign.getTicket(id);
        UserMovieDto userMovieDto = (UserMovieDto) ticket.getObject();

        JsonResult movieFeignById = movieFeign.findById(userMovieDto.getMovieTicketId());
        MovieTicket movieTicket = (MovieTicket) movieFeignById.getObject();
        //获取电影票的开始和结束时间
        Date satrtTime = movieTicket.getSatrt_time();
        long satrtTimeTime = satrtTime.getTime();
        Date endTime = movieTicket.getEnd_time();
        long endTimeTime = endTime.getTime();

        //获取到当前的时间
        long nowTime = new Date().getTime();
        //现在的时间已经大于结束时间
        if (nowTime > endTimeTime){
            ticketInfo.setMovieName(movieTicket.getName());
            ticketInfo.setMsg("电影已过期,不可观看");
            ticketInfo.setState(1);
            return ticketInfo;
            //现在的时间已经小于开始时间
        }else if (nowTime <satrtTimeTime){
            ticketInfo.setMovieName(movieTicket.getName());
            ticketInfo.setMsg("电影未开始");
            ticketInfo.setState(0);
            return ticketInfo;
        }else {
            ticketInfo.setMovieName(movieTicket.getName());
            ticketInfo.setMsg("电影已经开始");
            ticketInfo.setState(0);
            return ticketInfo;
        }


    }

    @Override
    public void addUser(User user) {
        //新增用户
        userMapper.addUser(user);
        // 随机赠送电影票
        double movieId = Math.random() * 10;
        String strId = String.valueOf(movieId);
        Long mid = Long.valueOf(strId);

        //购买电影票
        UserMovieDto userMovieDto = new UserMovieDto();
        userMovieDto.setMovieTicketId(mid);
        userMovieDto.setUserId(user.getId());
        userMovieDto.setCount(2);

        movieFeign.buyTicket(userMovieDto);

        //获取电影信息
        JsonResult byId = movieFeign.findById(userMovieDto.getMovieTicketId());
        MovieTicket movieTicket = (MovieTicket) byId.getObject();

    }


}
